<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

require_once 'vars.php';
require_once 'class.upload.php';

if (!empty($_FILES['Filedata'])
		&& isset($_POST['folder'])
		&& isset($_POST['filename'])
		&& isset($_POST['width'])
		&& isset($_POST['height'])) {
	$targetPath = $_SERVER['DOCUMENT_ROOT'].$sitePath.$imageRoot.$_POST['folder'];

	$handle = new Upload($_FILES['Filedata']);
	if ($handle->uploaded) {
		// save uploaded image with a new name
		$handle->file_new_name_body 	= $_POST['filename'];
		$handle->file_overwrite			= true;
		$handle->file_auto_rename		= false;
		$handle->image_resize			= true;
		$handle->image_x				= 475;//($_POST['width'] * 80) - 5;
		$handle->image_y				= 395;//($_POST['height'] * 80) - 5;
		$handle->image_ratio			= true;
		$handle->jpeg_quality			= 100;
		$handle->Process($targetPath);
		if ($handle->processed) {
			$json = array('result' 			=> 1,
							'dir'			=> $handle->file_dst_path,
							'filename'		=> $handle->file_dst_name,
							'pathname'		=> $_POST['folder'].$handle->file_dst_name,
							'imagewidth' 	=> $handle->image_dst_x,
							'imageheight'	=> $handle->image_dst_y,
							'response'		=> 'image "'.$_FILES['filename'].'" uploaded and renamed to "'.$_POST['filename'].'".'
							);
		} else {
			$json = array('result' 		=> 0,
							'response'	=> 'image processing failed!\n'
											.$handle->error
			);
		}
	} else {
		$json = array('result' => 0,
						'response'	=> 'upload failed!\n'.$handle->error
		);
	}
	$handle->clean();
} else {
	$json = array('result' => 0, 'response'	=> 'parameters missing\n'
												.$handle->error
		);
}
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>